package com.android.BluethootChess.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.RelativeLayout;

import com.android.BluethootChess.control.TouchController;
import com.android.BluethootChess.model.GameBoardMatrix;
import com.android.BluethootChess.ui.ChessBoard;
import com.android.BluethootChess.ui.UISquares;
import com.android.BluethootChess.util.Utils;

public class BluethootChess extends Activity {

	private static final int[] boardColors = new int[2];
	
	private void initData() {
		Utils.BOARD_HEIGHT = getResources()
				.getInteger(R.integer.BoardHeight);
		Utils.BOARD_WIDTH = getResources().getInteger(R.integer.BoardWidth);
		Utils.COLOR_TO_IGNORE = getResources().getInteger(
				R.integer.ColorToIgnore);

		Utils.HIGHLIGHT_COLOR = getResources().getInteger(
				R.integer.HighlightColor);

		boardColors[0] = getResources().getInteger(R.integer.BoardColor1);
		boardColors[1] = getResources().getInteger(R.integer.BoardColor2);
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initData();

		Context context = getApplicationContext();
		RelativeLayout r = new RelativeLayout(context);
		GameBoardMatrix gameMatrix=new GameBoardMatrix();
		gameMatrix.newGame();
		
		UISquares squares=new UISquares();
		TouchController controller=new TouchController(squares ,boardColors,gameMatrix);
		ChessBoard board = new ChessBoard(context,controller , boardColors,gameMatrix);

		r.addView(board);
		setContentView(r);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			Intent myIntent = new Intent(BluethootChess.this,
					NewGameActivity.class);
			BluethootChess.this.startActivity(myIntent);
			quitProcess();
		}

		return super.onKeyDown(keyCode, event);
	}
    
    protected void quitProcess() {
 	   super.onDestroy();
 	   android.os.Process.killProcess(android.os.Process.myPid());
 	}
	
}